package com.wwp.tbshop.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;

/**
 * Created by ${wenping} on 6/20/2017.
 */

public class SplashActivity extends Activity {

    private static final long TIME = 3000;
    private SparseArray<CountDownTimer> countDownMap;
    private ImageView mIvWelcome;
    private TextView mTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mIvWelcome = (ImageView) findViewById(R.id.iv_welcome);
        mTime = (TextView) findViewById(R.id.time);
        initAnimation();
    }

    private void initAnimation() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,1.0f);
        alphaAnimation.setDuration(TIME);
        mIvWelcome.startAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(listener);
        countDownMap = new SparseArray<>();
        /**倒计时*/
        if (TIME>0){
            CountDownTimer countDownTimer = new CountDownTimer(TIME, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    mTime.setText(millisUntilFinished/1000+"");
                }

                @Override
                public void onFinish() {
                    mTime.setVisibility(View.GONE);

                }
            }.start();

            countDownMap.put(mTime.hashCode(),countDownTimer);
        }else {
            mTime.setVisibility(View.GONE);
        }
    }
    private boolean isFirstTimeStart = true;
    Animation.AnimationListener listener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (UiUtil.getBoolean(SplashActivity.this, Constant.SPLASH_KEY)){
                Intent intent = new Intent(SplashActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }else {
                Intent intent = new Intent(SplashActivity.this,WelcomeAPagerctivity.class);
                startActivity(intent);
                finish();
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };
}